package com.sc.nft.admin.rpc;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.sc.nft.admin.request.DaoTransferRequest;
import com.sc.nft.admin.request.MallDaoWalletDetailPageRequest;
import com.sc.nft.dao.UserCreatorInfoDao;
import com.sc.nft.dao.UserInfoDao;
import com.sc.nft.dto.IdRequest;
import com.sc.nft.entity.*;
import com.sc.nft.entity.vo.MallDaoWalletDetailVO;
import com.sc.nft.entity.vo.MallDaoWalletSimpleVO;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.service.DaoTransferService;
import com.sc.nft.service.MallDaoWalletService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author liGen
 * @version 1.0
 * @date 2024/1/25 5:29 下午
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/rpc/admin/mall/daoWallet")
@Api(tags = "内部支付接口")
public class MallDaoWalletRpc {
    private final UserCreatorInfoDao userCreatorInfoDao;
    private final RedisTemplate redisTemplate;
    private final UserInfoDao userInfoDao;
    private final DaoTransferService daoTransferService;
    private final MallDaoWalletService mallDaoWalletService;


    @ApiOperation(value = "转账第三方钱包", notes = "转账第三方钱包", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/transferToThird", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result transferToThird(@Valid @RequestBody DaoTransferRequest request) {
        if(StrUtil.isBlank(request.getVerifyCode())){
            return Result.fail(ErrorCode.VERIFICATION_ERROR);
        }
        UserCreatorInfo userCreatorInfo = userCreatorInfoDao.getById(request.getDaoId());
        if(StringUtils.isBlank(userCreatorInfo.getPhoneNo())){
            return Result.fail("未设置验证手机号");
        }

//        UserInfo userInfo = userInfoDao.getById(userCreatorInfo.getUserId());
        Object errorTime = redisTemplate.opsForValue().get(RedisKeyEnum.DAO_PAY_SMS_CODE_ERROR_TIME.getKey() + userCreatorInfo.getPhoneNo());
        if(errorTime!=null&&Integer.parseInt(String.valueOf(errorTime))>5){
            return Result.fail("尝试次数过多，请十分钟后再试");
        }
        Object code = redisTemplate.opsForValue().get(RedisKeyEnum.DAO_PAY_SMS_CODE.getKey() + userCreatorInfo.getPhoneNo());
        if(!StrUtil.equals(request.getVerifyCode(),String.valueOf(code))){
            redisTemplate.opsForValue().increment(RedisKeyEnum.DAO_PAY_SMS_CODE_ERROR_TIME.getKey() + userCreatorInfo.getPhoneNo());
            redisTemplate.expire(RedisKeyEnum.DAO_PAY_SMS_CODE_ERROR_TIME.getKey() + userCreatorInfo.getPhoneNo(),10L, TimeUnit.MINUTES);
            return Result.fail(ErrorCode.VERIFICATION_ERROR);
        }
        daoTransferService.mallDaoApplyToThird(request.getId(),request.getDaoId(),request.getOperatorId(),request.getAmount());
        redisTemplate.delete(RedisKeyEnum.DAO_PAY_SMS.getKey()+userCreatorInfo.getPhoneNo());
        return Result.success();
    }

    @ApiOperation(value = "dao钱包明细", notes = "dao钱包明细", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/walletDetailPage", produces = MediaType.APPLICATION_JSON_VALUE)
//    @ApiIgnore
    public PageResult<MallDaoWalletDetailVO> walletDetailPage(@Valid @RequestBody MallDaoWalletDetailPageRequest request) {

        Page<MallDaoWalletDetail> mallDaoWalletDetailPage = mallDaoWalletService.getDaoWalletDetailPage(request.getDaoId(),request.getType(), request.getStartTime(), request.getEndTime(), request.toMp(),request.getIsActivity());
        Page<MallDaoWalletDetailVO> page = new Page<>(mallDaoWalletDetailPage.getCurrent(),mallDaoWalletDetailPage.getSize(),mallDaoWalletDetailPage.getTotal());
        List<MallDaoWalletDetailVO> result = Lists.newArrayList();
        for (MallDaoWalletDetail record : mallDaoWalletDetailPage.getRecords()) {
            MallDaoWalletDetailVO mallDaoWalletDetailVO = new MallDaoWalletDetailVO();
            mallDaoWalletDetailVO.setAmount(record.getAmount());
            mallDaoWalletDetailVO.setBeforeAmount(record.getBeforeAmount());
            mallDaoWalletDetailVO.setAfterAmount(record.getAfterAmount());
            mallDaoWalletDetailVO.setType(record.getType());
            mallDaoWalletDetailVO.setTypeTxt(record.getType().getDescription());
            mallDaoWalletDetailVO.setRemark(record.getRemark());
            mallDaoWalletDetailVO.setIsAdd(record.getIsAdd());
            mallDaoWalletDetailVO.setCreateTime(record.getCreateTime());
            result.add(mallDaoWalletDetailVO);
        }
        page.setRecords(result);
        return PageResult.success(page);
    }

    @ApiOperation(value = "查询DAO钱包余额", notes = "查询DAO钱包余额", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getWalletInfo", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<MallDaoWalletSimpleVO> getWalletInfo(@Valid @RequestBody IdRequest request) {
        MallDaoWallet mallDaoWallet = mallDaoWalletService.getByDaoId(request.getId());
        return Result.success(mallDaoWallet, MallDaoWalletSimpleVO.class);
    }
}
